package sgg_hsp_data_structures._02_stack_queue;

import java.util.Scanner;

public class ArrayQueueDemo {

    public static void main(String[] args) {

        ArrayQueue arrayQueue = new ArrayQueue(3);

        char key = ' ';
        Scanner scanner = new Scanner(System.in);

        boolean loop = true;
        while (loop) {

            System.out.println("************WELCOME************");
            System.out.println("s(show):");
            System.out.println("e(exit):");
            System.out.println("a(add) :");
            System.out.println("g(get) :");
            System.out.println("h(head):");
            System.out.println("*******************************");

            key = scanner.next().charAt(0);
            switch (key) {
                case 's':
                    arrayQueue.showQueue();
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                case 'a':
                    int value = scanner.nextInt();
                    arrayQueue.addQueue(value);
                    break;
                case 'g':
                    int res = arrayQueue.getQueue();
                    System.out.println("get :" + res);
                    break;
                case 'h':
                    int res1 = arrayQueue.headQueue();
                    System.out.println("head :" + res1);
                    break;
                default:
                    break;
            }
        }

    }
}


/**
 * front 随输出变化
 * rear  随输入变化
 */
class ArrayQueue {
    private int maxSize;

    private int front;
    private int rear;

    private int[] arr;

    public ArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
        front = -1;
        rear = -1;

    }

    public void addQueue(int n) {
        if (isFull()) {
            System.out.println("Full");
            return;
        }
        rear++;
        arr[rear] = n;
    }

    public int getQueue() {
        if (isEmpty()) {
            System.out.println("Empty");
            throw new RuntimeException("Empty");
        }
        front++;
        return arr[front];
    }

    public void showQueue() {
        if (isEmpty()) {
            System.out.println("Empty");
        }

        for (int i = 0; i < arr.length; i++) {
            System.out.printf("arr[%d]=%d\n", i, arr[i]);
        }
    }

    public int headQueue() {
        if (isEmpty()) {
            System.out.println("Empty");
            throw new RuntimeException("Empty");
        }
        return arr[front + 1];
    }

    public boolean isFull() {
        return rear == maxSize - 1;
    }

    public boolean isEmpty() {
        return front == rear;
    }

}
